package co.fitstart.admin.entity.search;

import java.util.Map;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.course.CourseType;

/**
 * 
 * @author lidan
 *
 */
public class SiteStudentSearch implements QueryBuilder {
    
    private String name;
    
    private String mobile;
    
    private String type;
    
    private Long siteId;
    
    private Order order;
    
    private Filter filter;
    
    public SiteStudentSearch() {
       order = new Order("round.student.createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        
        
        if(filter == null || refresh) {
            
            filter = new Filter().addEquals("round.courseType", CourseType.aliasOf(type))
                                 .addEquals("round.site.id", siteId)
                                 .addLike("round.student.name", name)
                                 .addLike("round.student.mobile", mobile)
                                 .addGreaterThan("round.student.stat." + type + "Count", 0);

        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "SELECT round.student FROM CourseRound round WHERE round.disabled = false AND round.student.disabled = false " + getPreparedFilter(false).getClauseSentence() + " GROUP BY round.student " + order.getSentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @return the siteId
     */
    public Long getSiteId() {
        return siteId;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @param siteId the siteId to set
     */
    public void setSiteId(Long siteId) {
        this.siteId = siteId;
    }

}
